use actix_web::{HttpResponse, ResponseError};
use serde::Serialize;
use thiserror::Error;

#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    pub message: String,
}

#[derive(Debug, Error)]
pub enum ApiError {
    #[error("Bad request: {0}")]
    BadRequest(String),
    #[error("Not found: {0}")]
    NotFound(String),
    #[error("Internal error")]
    Internal,
}

impl ResponseError for ApiError {
    fn error_response(&self) -> HttpResponse {
        match self {
            ApiError::BadRequest(m) => HttpResponse::BadRequest().json(ErrorResponse { message: m.clone() }),
            ApiError::NotFound(m) => HttpResponse::NotFound().json(ErrorResponse { message: m.clone() }),
            ApiError::Internal => HttpResponse::InternalServerError().json(ErrorResponse { message: "Internal error".into() }),
        }
    }
}